<?xml version="1.0" encoding="UTF-8"?>
{%- if xml_version == "05" %}
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="">
{%- elif xml_version == "03CH02" %}
<Document xmlns="http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd  pain.001.001.03.ch.02.xsd">
{%- else %}
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="">
{%- endif %}    
  <CstmrCdtTrfInitn>
    <GrpHdr>
      <MsgId>{{ msgid }}</MsgId>
      <CreDtTm>{{ date }}</CreDtTm>
      <NbOfTxs>{{ "{:d}".format(transaction_count) }}</NbOfTxs>
      <CtrlSum>{{ "{:.2f}".format(control_sum) }}</CtrlSum>
      <InitgPty>
        <Nm>{{ company.name }}</Nm>
        {%- if company.address_line1 %}
        <!-- <PstlAdr>
            <Ctry>{{ company.country_code }}</Ctry>
            <AdrLine>{{ company.address_line1 }}</AdrLine>
            <AdrLine>{{ company.address_line2 }}</AdrLine>
        </PstlAdr> -->
        {%- endif %}
      </InitgPty>
    </GrpHdr>
    <PmtInf>
      <PmtInfId>{{ payments[0].id }}</PmtInfId>
      <PmtMtd>{{ payments[0].method }}</PmtMtd>
      {%- if payments[0].service_level == "SEPA" %}
      <PmtTpInf>
        <SvcLvl><Cd>SEPA</Cd></SvcLvl>
      </PmtTpInf>
      {%- else %}
      <BtchBookg>{{ payments[0].batch }}</BtchBookg>
      <NbOfTxs>{{ "{:d}".format(transaction_count) }}</NbOfTxs>
      <CtrlSum>{{ "{:.2f}".format(control_sum) }}</CtrlSum>
      {%- endif %}
      {%- if xml_region == "CH" and payments[0].is_salary and not payments[0].service_level == "SEPA" %}{# CH only: salary information #}
      <PmtTpInf>
        <CtgyPurp>
          <Cd>SALA</Cd>
        </CtgyPurp>
      </PmtTpInf>
      {%- endif %}
      <ReqdExctnDt>{{ payments[0].required_execution_date }}</ReqdExctnDt>
      <Dbtr>
        <Nm>{{ company.name }}</Nm>
        {%- if company.address_line1 %}
        <PstlAdr>
            <Ctry>{{ company.country_code }}</Ctry>
            <AdrLine>{{ company.address_line1 }}</AdrLine>
            <AdrLine>{{ company.address_line2 }}</AdrLine>
        </PstlAdr>
        {%- endif %}
      </Dbtr>
      <DbtrAcct>
        <Id>
          <IBAN>{{ company.iban }}</IBAN>
        </Id>
        {%- if xml_region == "CH" and payments[0].is_salary %}{# CH only: salary information: confidential #}
        <Tp><Prtry>NOA</Prtry></Tp>
        {%- endif %}
      </DbtrAcct>
      <DbtrAgt>
          <FinInstnId>
              {%- if xml_region == "CH" %}{# CH only: BIC mandatory #}
              <BIC>{{ company.bic }}</BIC>
              {%- else %}
              <Othr><Id>NOTPROVIDED</Id></Othr>
              {%- endif %}
          </FinInstnId>
      </DbtrAgt>
      {%- for payment in payments %}
      <CdtTrfTxInf>
        <PmtId>
          {%- if xml_region == "CH" %}{# CH only: instruction ID mandatory #}
          <InstrId>{{ payment.id }}</InstrId>
          {%- endif %}
          <EndToEndId>{{ payment.end_to_end_id }}</EndToEndId>
        </PmtId>
        {%- if payment.service_level == "ESR" %}
        <PmtTpInf>
          <LclInstrm><Prtry>CH01</Prtry></LclInstrm>
        </PmtTpInf>
        {%- endif %}
        <Amt>
          <InstdAmt Ccy="{{ payment.currency }}">{{ "{:.2f}".format(payment.amount) }}</InstdAmt>
        </Amt>
        {%- if payment.service_level == "IBAN" and xml_region == "CH" and payment.iban[0:2] != "CH" and payment.currency != "EUR" %}{# type 6 payment for USD #}
        <CdtrAgt>
            <FinInstnId><BIC>{{ payment.bic }}</BIC></FinInstnId>
        </CdtrAgt>
        {%- endif %}
        <Cdtr>
          <Nm>{{ payment.creditor.name }}</Nm>
          <PstlAdr>
            <Ctry>{{ payment.creditor.country_code }}</Ctry>
            <AdrLine>{{ payment.creditor.address_line1 }}</AdrLine>
            <AdrLine>{{ payment.creditor.address_line2 }}</AdrLine>
          </PstlAdr>
        </Cdtr>
        {%- if payment.service_level == "SEPA" %}
        {#- SEPA bapyment block #}
        <CdtrAcct>
          <Id>
            <IBAN>{{ payment.iban }}</IBAN>
          </Id>
        </CdtrAcct>
        <RmtInf>
          <Ustrd>{{ payment.reference }}</Ustrd>
        </RmtInf>
        {%- elif payment.service_level == "ESR" %}
        {#- ESR payment block #}
        <CdtrAcct>
        <Id><Othr><Id>{{ payment.esr_participation_number }}</Id></Othr></Id>
        </CdtrAcct>
        <RmtInf><Strd><CdtrRefInf><Ref>{{ payment.esr_reference }}</Ref></CdtrRefInf></Strd></RmtInf>
        {%- elif payment.service_level == "QRR" %}
        {#- QRR payment block #}
        <CdtrAcct>
        <Id><IBAN>{{ payment.esr_participation_number }}</IBAN></Id>
        </CdtrAcct>
        <RmtInf><Strd><CdtrRefInf><Tp><CdOrPrtry><Prtry>QRR</Prtry></CdOrPrtry></Tp><Ref>{{ payment.esr_reference }}</Ref></CdtrRefInf></Strd></RmtInf>
        {%- else %}
        {#- IBAN payment block #}
        <CdtrAcct>
          <Id>
            <IBAN>{{ payment.iban }}</IBAN>
          </Id>
        </CdtrAcct>
        <RmtInf>
          <Ustrd>{{ payment.reference }}</Ustrd>
        </RmtInf>        
        {%- endif %}
      </CdtTrfTxInf>
      {%- endfor %}
    </PmtInf>
  </CstmrCdtTrfInitn>
</Document>
